/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rpgmapper.gui;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.elements.render.ImageRenderer;
import de.lessvoid.nifty.render.NiftyImage;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import org.rpgmapper.manager.DrawingManager.Tool;
import org.rpgmapper.tools.AppContext;

/**
 *
 * @author Nehon
 */
public class DrawGuiController implements ScreenController {

    private Nifty nifty;
    private Screen screen;
    NiftyImage pen;
    NiftyImage penS;
    NiftyImage rubber;
    NiftyImage rubberS;
    NiftyImage magnet;
    NiftyImage magnetS;
    NiftyImage move;
    NiftyImage moveS;
    Element confirmDialog;

    public void bind(Nifty nifty, Screen screen) {
        this.nifty = nifty;
        this.screen = screen;
//        pen = nifty.getRenderEngine().createImage("Textures/Icons/Pencil.png", false); // false means don't linear filter the image, true would apply linear filtering
//        penS = nifty.getRenderEngine().createImage("Textures/Icons/PencilS.png", false); // false means don't linear filter the image, true would apply linear filtering
//        rubber = nifty.getRenderEngine().createImage("Textures/Icons/Rubber.png", false); // false means don't linear filter the image, true would apply linear filtering
//        rubberS = nifty.getRenderEngine().createImage("Textures/Icons/RubberS.png", false); // false means don't linear filter the image, true would apply linear filtering
//        magnet = nifty.getRenderEngine().createImage("Textures/Icons/Magnet.png", false); // false means don't linear filter the image, true would apply linear filtering
//        magnetS = nifty.getRenderEngine().createImage("Textures/Icons/MagnetS.png", false); // false means don't linear filter the image, true would apply linear filtering
//        move = nifty.getRenderEngine().createImage("Textures/Icons/Move.png", false); // false means don't linear filter the image, true would apply linear filtering
//        moveS = nifty.getRenderEngine().createImage("Textures/Icons/MoveS.png", false); // false means don't linear filter the image, true would apply linear filtering
        confirmDialog = nifty.createPopup("confirmDialog");
    }

    public void onStartScreen() {
    }

    public void onEndScreen() {
    }

    public void pen() {
        //System.out.println("pen");
        if (AppContext.getMapManager().getDrawingManager().activateTool(Tool.Pen)) {
            screen.findElementByName("penImg").getRenderer(ImageRenderer.class).setImage(penS);
            screen.findElementByName("rubImg").getRenderer(ImageRenderer.class).setImage(rubber);
        } else {
            screen.findElementByName("penImg").getRenderer(ImageRenderer.class).setImage(pen);
        }
    }

    public void rubber() {
        //System.out.println("rubber");
        if (AppContext.getMapManager().getDrawingManager().activateTool(Tool.Rubber)) {
            screen.findElementByName("penImg").getRenderer(ImageRenderer.class).setImage(pen);
            screen.findElementByName("rubImg").getRenderer(ImageRenderer.class).setImage(rubberS);
        } else {
            screen.findElementByName("rubImg").getRenderer(ImageRenderer.class).setImage(rubber);
        }

    }

    public void snap() {
        //System.out.println("snap");

        if (AppContext.getMapManager().isSnapToGrid()) {
            screen.findElementByName("magImg").getRenderer(ImageRenderer.class).setImage(magnet);
            AppContext.getMapManager().setSnapToGrid(false);
        } else {
            screen.findElementByName("magImg").getRenderer(ImageRenderer.class).setImage(magnetS);
            AppContext.getMapManager().setSnapToGrid(true);
        }
    }

//    public void move() {
//        if (JmeMain.getApplicationContext().getRtsCam().getMode() == Mode.Pan) {
//            JmeMain.getApplicationContext().getRtsCam().setMode(Mode.Rotate);
//            screen.findElementByName("movImg").getRenderer(ImageRenderer.class).setImage(move);
//        } else {
//            JmeMain.getApplicationContext().getRtsCam().setMode(Mode.Pan);
//            screen.findElementByName("movImg").getRenderer(ImageRenderer.class).setImage(moveS);
//        }
//    }
    public void clear() {
        nifty.showPopup(screen, confirmDialog.getId(), null);
    }

    public void ok() {
        AppContext.getMapManager().getDrawingManager().clear();

        nifty.closePopup(confirmDialog.getId());
    }

    public void cancel() {
        nifty.closePopup(confirmDialog.getId());
    }
}
